package org.example.wumeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.wumeng.mapper.T_bookCategoryMapper;
import org.example.wumeng.pojo.BookCategory;
import org.example.wumeng.service.T_bookCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 医书分类服务实现类
 */
@Service
public class T_bookCategoryServiceImpl extends ServiceImpl<T_bookCategoryMapper, BookCategory> implements T_bookCategoryService {

    @Autowired
    private T_bookCategoryMapper bookCategoryMapper;

    /**
     * 获取所有启用的医书分类
     */
    @Override
    public List<BookCategory> getActiveCategories() {
        QueryWrapper<BookCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1)
                .orderByAsc("sort_order");
        return list(wrapper);
    }

    /**
     * 创建医书分类
     */
    @Override
    public Long createCategory(BookCategory category) {
        category.setStatus(1);
        if (category.getSortOrder() == null) {
            category.setSortOrder(0);
        }
        save(category);
        return category.getId();
    }

    /**
     * 更新医书分类
     */
    @Override
    public void updateCategory(Long id, BookCategory category) {
        category.setId(id);
        updateById(category);
    }

    /**
     * 删除医书分类
     */
    @Override
    public void deleteCategory(Long id) {
        removeById(id);
    }

    /**
     * 获取医书分类统计
     */
    @Override
    public List<Map<String, Object>> getCategoryStatistics() {
        return bookCategoryMapper.getCategoryStatistics();
    }
    @Override
    public List<Map<String, Object>> getBooksByCategory(String categoryName) {
        return bookCategoryMapper.getBooksByCategory(categoryName);
    }
}
